package com.yksj.healthtalk.ui.home;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartControlClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.server.ServerCenterMainUI;
//家园 商户收藏
public class MyServerCollectActivity extends BaseFragmentActivity implements OnClickListener, OnItemClickListener {

	private ListView listView;
	private ListAdapter adapter;
	private DisplayMetrics metrics;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.my_server_collection);
		initView();
	}

	private void initView() {
		initTitle();
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("我的商家");
		listView = (ListView) findViewById(R.id.listview);
		adapter = new ListAdapter(this);
		listView.setOnItemClickListener(this);
		TextView view=new TextView(MyServerCollectActivity.this);
		view.setText("没有收藏商家");
		view.setGravity(Gravity.CENTER);
		listView.setEmptyView(view);
		listView.setAdapter(adapter);
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}
	
	
	
	private void initData() {
		RequestParams params=new RequestParams();
		params.put("CUSTOMER_ID", SmartControlClient.getControlClient().getUserId());
		params.put("Type", "queryCollectionCenter33");
		HttpRestClient.doHttpServerCollect(params, new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
				adapter.onBountData(response.getJSONArray("message"));
			}
		});
		
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	class ListAdapter extends BaseAdapter{
		private final LayoutInflater inflater;
		private ImageLoader mLoader;
		private JSONArray array=new JSONArray();
		public ListAdapter(Context context) {
			inflater=LayoutInflater.from(context);
			mLoader = ImageLoader.getInstance();
		}

		@Override
		public int getCount() {
			return array.size();
		}

		@Override
		public Object getItem(int position) {
			return array.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public void onBountData(JSONArray array){
			if(array.isEmpty())return;
			this.array.clear();
			this.array.addAll(array);
			notifyDataSetChanged();
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			JSONObject jsonObject= (JSONObject) getItem(position);
			if(convertView==null){
				convertView=inflater.inflate(R.layout.main_seach_all_item, null);
			}
			TextView textView=(TextView) convertView.findViewById(R.id.name_type);
			ImageView imageView=(ImageView)convertView.findViewById(R.id.icon_view);
			
			textView.setText( jsonObject.getString("MERCHANT_NAME"));
			mLoader.displayImage(jsonObject.getString("COLLECTION_ICON"), imageView);
			return convertView;
		}
		
	}
	
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		final JSONObject entity=  (JSONObject) adapter.getItem(position);
		HttpRestClient.doHttpServerCneterBg(String.valueOf(metrics.widthPixels), String.valueOf(metrics.heightPixels), SmartFoxClient.getLoginUserId(),entity.getString("MERCHANT_ID"),
				new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode,JSONObject response) {
				super.onSuccess(statusCode, response);
				String str = response.getString("VALID_FLAG");
				if ("2".equals(str)) {// 不可以进入
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), "即将开通,敬请期待!");
				} else {
					Intent intent=new Intent(MyServerCollectActivity.this,ServerCenterMainUI.class);
					Bundle bundle=new Bundle();
					bundle.putString("content", response.toString());
					bundle.putString("MERCHANT_ID",entity.getString("MERCHANT_ID"));
					intent.putExtras(bundle);
					startActivity(intent);
				}
			}
		});
		
	
	}
	
}
